package edu.cmu.cs.stage3.alice.authoringtool.util;

import java.awt.Toolkit;

import edu.cmu.cs.stage3.alice.authoringtool.py.AliceStyle;

public class AliceTabbedPaneUI extends javax.swing.plaf.metal.MetalTabbedPaneUI {

 protected java.awt.Color defaultTabForeground;

 public AliceTabbedPaneUI () {
  setTabAreaInsets( new java.awt.Insets( 0, 0, 0, 0 ) );
  }

 //Rem @Override
 protected void installDefaults () {
  super.installDefaults();

  this.selectColor = java.awt.Color.white;
  this.selectHighlight = java.awt.Color.black;
  this.tabAreaBackground = java.awt.Color.white;
  this.darkShadow = java.awt.Color.black;
  this.focus = new java.awt.Color( 255, 255, 255, 0 ); // don't paint focus
  this.highlight = java.awt.Color.darkGray; // set the color of the tab off focus

  //new for 1.4
  this.contentBorderInsets = new java.awt.Insets( 1, 1, 1, 1 );

  // Depend on JVM: TabbedPane.foreground=[(0,0,0), (51,51,51)] and TabbedPane.background=[(153,153,153), (184,207,229)]
  // defaultTabForeground = AliceStyle.fgColor; // javax.swing.UIManager.getColor("TabbedPane.foreground");
  defaultTabForeground = javax.swing.UIManager.getColor("TabbedPane.foreground"); // IMPORTANTE: without this => do not render panels!!
  }

 public void setTabAreaInsets (java.awt.Insets insets) {
  this.tabAreaInsets = insets;
  }

 //Rem @Override
 public void update (java.awt.Graphics g, javax.swing.JComponent c) {
  int tabPlacement = tabPane.getTabPlacement();
  java.awt.Insets insets = c.getInsets();
  java.awt.Dimension size = c.getSize();

  // match tab color to the component's background color
  java.awt.Component selectedComponent = this.tabPane.getSelectedComponent();
  if (selectedComponent != null) {
   this.selectColor = this.tabAreaBackground = selectedComponent.getBackground();
   }

  g.setColor( tabAreaBackground );
  int tabAreaHeight = calculateTabAreaHeight( tabPlacement, runCount, maxTabHeight );
  g.fillRect( insets.left, insets.top + tabAreaHeight, size.width - insets.right - insets.left, size.height - tabAreaHeight );

  paint( g, c );
  }

 /**
  * overridden for tab fonts
  */
 //Rem @Override
 protected void paintTab (java.awt.Graphics g, int tabPlacement, java.awt.Rectangle[] rects, int tabIndex, java.awt.Rectangle iconRect, java.awt.Rectangle textRect) {
  java.awt.Rectangle tabRect = rects[tabIndex];
  int selectedIndex = tabPane.getSelectedIndex();
  boolean isSelected = selectedIndex == tabIndex;
  paintTabBackground( g, tabPlacement, tabIndex, tabRect.x, tabRect.y, tabRect.width, tabRect.height, isSelected );
  paintTabBorder( g, tabPlacement, tabIndex, tabRect.x, tabRect.y, tabRect.width, tabRect.height, isSelected );

  // for dark tabs, draw text in white
  if (isSelected && (tabPane.getSelectedComponent() != null)) {
   java.awt.Color background = tabPane.getSelectedComponent().getBackground();
   int brightness = (background.getRed() + background.getGreen() + background.getBlue())/3;
   if (brightness < 128) {
    if (! tabPane.getForegroundAt( tabIndex ).equals( java.awt.Color.white )) {
     tabPane.setForegroundAt( tabIndex, java.awt.Color.white );
     }
   } else {
    if (! tabPane.getForegroundAt( tabIndex ).equals( defaultTabForeground )) {
     tabPane.setForegroundAt( tabIndex, null );
     }
    }
  } else {
   if (! tabPane.getForegroundAt( tabIndex ).equals( defaultTabForeground )) {
    tabPane.setForegroundAt( tabIndex, null );
    }
   }

  String title = tabPane.getTitleAt( tabIndex );
  java.awt.Font font = tabPane.getFont();
  if (isSelected) {
   font = font.deriveFont( java.awt.Font.BOLD );
  } else {
   font = font.deriveFont( java.awt.Font.PLAIN, 12 ); //size 12 is a hack here
   }
  java.awt.FontMetrics metrics = g.getFontMetrics( font );

  javax.swing.Icon icon = null;
  try { icon = getIconForTab( tabIndex ); }
  catch (Exception e) { System.err.println("Error: AliceTabbedPaneUI.java: paintTab(...): tabIndex="+tabIndex); e.printStackTrace(); }

  layoutLabel( tabPlacement, metrics, tabIndex, title, icon, tabRect, iconRect, textRect, isSelected );
  paintText( g, tabPlacement, font, metrics, tabIndex, title, textRect, isSelected );
  paintIcon( g, tabPlacement, tabIndex, icon, iconRect, isSelected );
  paintFocusIndicator( g, tabPlacement, rects, tabIndex, iconRect, textRect, isSelected );
  }

 public int getTabAreaHeight () {
  java.awt.Insets tabAreaInsets = getTabAreaInsets( tabPane.getTabPlacement() );
  int runCount = tabPane.getTabRunCount();
  int tabRunOverlay = getTabRunOverlay( tabPane.getTabPlacement() );
  return (runCount > 0 ? runCount * (maxTabHeight-tabRunOverlay) + tabRunOverlay + tabAreaInsets.top + tabAreaInsets.bottom : 0);
  }

 public int getTabAreaHeightIgnoringInsets () {
  int runCount = tabPane.getTabRunCount();
  int tabRunOverlay = getTabRunOverlay( tabPane.getTabPlacement() );
  return (runCount > 0 ? runCount * (maxTabHeight-tabRunOverlay) + tabRunOverlay : 0);
  }

 //Rem @Override
 //Rem @SuppressWarnings("deprecation")
 protected java.awt.FontMetrics getFontMetrics () {
  java.awt.Font font = tabPane.getFont().deriveFont( java.awt.Font.BOLD );
  return Toolkit.getDefaultToolkit().getFontMetrics( font );
  }

 }
